home *** CD-ROM | disk | FTP | other *** search
- Path: crchh327.rich.bnr.ca!jobell
- From: jobell@bnr.ca (Bret Bieghler)
- Newsgroups: comp.lang.c++
- Subject: Re: To find the class of an object ??
- Date: 27 Feb 1996 20:21:08 GMT
- Organization: Bell-Northern Research Ltd.
- Message-ID: <4gvp3k$j1@crchh327.rich.bnr.ca>
- References: <4gvel6$4vg@cf01>
- NNTP-Posting-Host: crchh524.rich.bnr.ca
-
- In article <4gvel6$4vg@cf01>, Didier BOLF <didier@cln46ib> wrote:
- >
- >Hi,
- >
- >My problem:
- >
- >I have a class A and a class B. The class B inherit the class A.
- >I have too: A tab[MAX];
- >In tab, I put objets of class A or B.
- >
- >How compare two objects of tab to know if they are of the same class ??
- >and which class they belong to ??
- >
- >Thanks to reply..
- >
- >Didier.
- >Email: didier@cln46fw.der.edf.fr
- >
-
- From Bruce Eckel's "Thinking in C++", page 721,
-
- "Run-time type identification (RTTI) lets you find the exact type
- of an object when you have only a pointer or reference to the base
- type."
-
- If your compiler supports RTTI look into this, or you can add
- RTTI member functions that return identifiers as to what type they
- are.
-
- As in:
-
- B myBObject;
- A* aPtr;
-
- aPtr = &myBObject;
-
- aPtr->whatType();
-
- would print (or return) "I'm a B."
-
- thus, you would have
-
- class A
- {
- virtual void whatType(void)
- {
- cout << "I'm an A." << endl;
- }
- }
-
- class B : public A
- {
- void whatType (void)
- {
- cout << "I'm a B." << endl;
- }
- }
-
- hope this helps.
-
- Joe
-
- --
- Joseph A. Bell (NOT Bret Bieghler) jobell@bnr.ca
- Northern Telecom / Bell-Northern Research
- "What? Evacuate now, in our moment of triumph? Surely you overestimate their chances."
-